package com.itheima.controller;

import com.itheima.pojo.entity.PageResult;
import com.itheima.pojo.entity.Result;
import com.itheima.pojo.entity.Student;
import com.itheima.pojo.query.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zzw
 * @date 2025/4/27
 * @description 学员管理
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学员信息
     *
     * @param studentQueryParam 查询参数
     * @return Result
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询员工信息，参数：{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }


    /**
     * 删除学员信息
     *
     * @param ids 学员id数组
     * @return Result
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        log.info("根据id删除学员, id: {}", Arrays.asList(ids));
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 新增学员信息
     *
     * @param student 学员信息
     * @return Result
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("保存员工信息，参数：{}", student);
        studentService.insert(student);
        return Result.success();
    }

    /**
     * 根据id查询学员信息
     *
     * @param id 学员id
     * @return Result
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工信息，参数：{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     *
     * @param student 学员信息
     * @return Result
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改员工信息，参数：{}", student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 学员违纪处理
     *
     * @param id    学员id
     * @param score 违规分数
     * @return Result
     */
    @PutMapping("/violation/{id}/{score}")
    public Result handleViolation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("学员违纪处理，参数：{},{}", id, score);
        studentService.handleViolation(id, score);
        return Result.success();
    }
}
